package yazan.com.pdacollector_android.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

import com.blankj.utilcode.util.StringUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.maning.mndialoglibrary.MProgressDialog;

import java.lang.ref.WeakReference;
import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import yazan.com.pdacollector_android.R;
import yazan.com.pdacollector_android.base.BaseActivity;
import yazan.com.pdacollector_android.base.Presenter;
import yazan.com.pdacollector_android.presenter.AccountPresenter;
import yazan.com.pdacollector_android.utils.MProgressDialogHelper;

public class ChangePassWordActivity extends BaseActivity {
    //<editor-fold desc=”成员变量”>

    @BindView(R.id.etUsername)
    EditText etUsername;
    @BindView(R.id.etOldPassWord)
    EditText etOldPassWord;
    @BindView(R.id.etNewPassWord)
    EditText etNewPassWord;


    private String baseUrl;

    //</editor-fold>

    //<editor-fold desc=”生命周期”>


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_change_pass_word);
        ButterKnife.bind(this);
        baseUrl = getIntent().getStringExtra("baseurl");
        configUI();
    }

    //</editor-fold>

    //<editor-fold desc=”私有方法”>

    private void configUI() {
        String username = getIntent().getStringExtra("username");
        etUsername.setText(username);
    }

    @OnClick({R.id.btConfim})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btConfim: {
                String msg = validationField();
                if (msg == null) {
                    MProgressDialogHelper.showLoading(this);
                    String userName = etUsername.getText().toString();
                    String oldPassWord = etOldPassWord.getText().toString();
                    String newPassWord = etNewPassWord.getText().toString();
                    AccountPresenter.changePassWord(baseUrl, userName, oldPassWord, newPassWord, 2, new ChangePassWordListener(this));
                } else {
                    ToastUtils.showShort(msg);
                }
            }
            break;
        }
    }

    /**
     * 验证表单
     *
     * @return .
     */
    private String validationField() {
        if (StringUtils.isEmpty(etUsername.getText())) {
            return "账号不能为空";
        }
        if (StringUtils.isEmpty(etOldPassWord.getText())) {
            return "老密码不能为空";
        }
        if (StringUtils.isEmpty(etNewPassWord.getText())) {
            return "新密码不能为空";
        }
        return null;
    }

    //</editor-fold>

    //<editor-fold desc=”内部类”>

    private static class ChangePassWordListener implements Presenter.PresenterListener<HashMap> {

        private WeakReference<ChangePassWordActivity> weakReference;

        public ChangePassWordListener(ChangePassWordActivity context) {
            this.weakReference = new WeakReference<>(context);
        }

        @Override
        public void onSuccess(HashMap object) {
            MProgressDialog.dismissProgress();

            final boolean flag = (boolean) object.get("Flag");
            if (weakReference.get() != null) {
                if (flag) {
                    MProgressDialogHelper.showSuccess(weakReference.get(), "修改成功", new MProgressDialogHelper.MProgressDialogHelperListener
                            () {
                        @Override
                        public void complete() {
                            if (weakReference.get() != null) {
                                weakReference.get().finish();
                            }
                        }
                    });
                } else {
                    MProgressDialogHelper.showFail(weakReference.get(), (String) object.get("Message"), null);
                }
            }
        }

        @Override
        public void onError(String error) {
            MProgressDialog.dismissProgress();
            if (weakReference.get() != null) {
                MProgressDialogHelper.showFail(weakReference.get(), "网络链接异常!", null);
            }
        }
    }

    //</editor-fold>
}
